//前台工厂图片业务代码
const factoryPicModel = require("../models/factoryPic.js");
const moment = require("moment");
class frFactoryController {
    //渲染页面
    static async showPage(req, res) {
        let { page = 1, size = 8 } = req.query;
        page = parseInt(page);
        size = parseInt(size);
        // let count = await factoryPicModel.count();
        let count = await factoryPicModel.countDocuments();

        let offset = (page - 1) * size;
        let totalPage = Math.ceil(count / size);
        let info = await factoryPicModel.find().limit(size).skip(offset)
        res.render("../views/front/factoryPic", { info, page, size, totalPage });
    }
    //图片详情
    static async showPicDetail(req, res) {
        // res.json(req.query)使用字符串获取参数
        //
        factoryPicModel.findOne({ _id: req.query.id }, async (error, data) => {
            if (error) {
                res.redirect("back");
            } else {
                data.hot += 1;
                await data.save()
                res.render("../views/front/picDetail", { info: data, moment })
            }
        })

    }
}
module.exports = frFactoryController
